// pages/02admin/10goodsCate/goodsCate.js
var app = getApp()
var appData = getApp().globalData
var co = require('../../js/common.js')
Page({
   data: {
      isShowInputLabelArr: [0, 0],
      goodsCateList: null,
      buttonText: '添加商品分类',
      pick_i: 0,
      edit_id: -1,
      mode: '',
      printerList: [],
      exchange_id: -1,
      sorting: '编辑排序'
   },
   sorting: function () {
      this.setData({
         mode: this.data.mode == '' ? 'sort' : '',
         sorting: this.data.mode == 'sort' ? '编辑排序' : '返回',
      })
   },
   addButtonChange: function () {
      this.setData({
         pick_i: 0,
         mode: this.data.mode == '' ? 'add' : '',
         buttonText: this.data.mode == '' ? '取消添加' : '添加商品分类',
         edit_id: -1,
         isShowInputLabelArr: [0, 0]
      })
   },
   //排序
   toExchange: function (e) {
      var td = this.data
      var t = this
      var index = e.currentTarget.id
      console.log('试试这个td===', td)
      console.log('试试这个e ===', e)
      if (td.exchange_id != -1 && index != '') {
         var param = {
            id1: td.goodsCateList[index].cateId,
            id2: td.goodsCateList[td.exchange_id].cateId,
            shop_id: appData.shopInfo.Biz_ID
         }
         //tp5
         app.getNetData('product/cate/exchangesort', param, function (res) {
            console.log('试试看', res)
            t.setData({
               goodsCateList: res.data,
               exchange_id: -1
            })
            appData.goodsCateList = res.data
         })
      } else {
         this.setData({ exchange_id: index == '' ? -1 : index })
      }

   },
   //跳到编辑
   editgoods: function (e) {
      var td = this.data
      var index = e.currentTarget.id
      var printer = td.printerList[index]
      this.setData({
         edit_id: td.edit_id == index ? -1 : index,
         isShowInputLabelArr: index > -1 ? [1, 1] : [0, 0],
         mode: '',
         buttonText: '添加商品分类',
      })
      console.log('this.data==', td)
   },
   //整理排序
   arrange: function (e) {
      var td = this.data
      var t = this
      var param = {
         shop_id: appData.shopInfo.Biz_ID
      }
      //tp5
      app.getNetData('table/cate/sortOut', param, function (res) {
         console.log('试试看', res)
         t.setData({ tableCateList: res.data })
         appData.tableCateArr = res.data
      })
   },
   //添加或修改
   submitGoodsInfo: function (e) {
      var td = this.data

      var t = this
      var param = e.detail.value

      param.uid = appData.shopInfo.Biz_ID
      console.log('我要试试效果', td)
      if (co.isEmpty(param.sort)) {
         param.sort = 0
      }

      if (td.edit_id > -1) {
         param.cate_id = td.goodsCateList[td.edit_id].cateId
         param.sql = 'update'
      } else {
         param.sql = 'insert'
      }

      app.getJsonData('api/admin/pro_cate_save.php', param, function (res) {
         console.log('waiter_save:res', res)
         if (td.edit_id == -1) {
            t.addButtonChange()
         } else {
            t.setData({ edit_id: -1 })
         }
         t.setData({ isShowInputLabelArr: [0, 0] })
         //通知更新
         app.noticeUpdateGoodsCate(res.data)
      })
   },
   onLoad: function (options) {
      var that = this
      app.setNoticeGoodsCatePage(function (updateData) {
         var isHave = 0
         var list = that.data.goodsCateList
         for (var i = 0; i < list.length; i++) {
            if (updateData.cateId == list[i].cateId) {
               list[i].name = updateData.name
               list[i].nums = updateData.nums
               isHave++
            }
         }
         if (!isHave) {
            list.push(updateData)
         }
         that.setData({ goodsCateList: list })
         appData.goodsCateList = list
         console.log(that.data.goodsCateList)
      })
      //提交更新
      if (appData.goodsCateList) {
         this.setData({ goodsCateList: appData.goodsCateList })
      } else {
         this.getGoodsCateData()
      }
   },

   getGoodsCateData: function () {
      var that = this
      var param = {
         shop_id: appData.shopInfo.Biz_ID,
      }
      app.getNetData('product/cate/getlist', param, function (goodsData) {
         that.handleGoodsData(goodsData)
      })
   },

   editGoods: function (e) {
      if (app.globalData.loginMode == 'boss') {
         let oneGoods;
         if (e.currentTarget.id != '') {
            oneGoods = JSON.stringify(this.data.goodsCateList[e.currentTarget.id]);
         }

         wx.navigateTo({
            url: '/pages/02admin/11setGoodsClass/goodsClass?goods=' + oneGoods
         })
      } else {
         app.toast('需要掌柜登录才能操作')
      }
   },
   delConfirm: function (e) {
      var t = this
      app.confirm('重要提示', '确认删除吗？', '删除', function (res) {
         console.log('delConfirm: function', res)
         if (res) {
            t.delGoods(e)
         }
      })
   },
   delGoods: function (e) {
      if (app.globalData.loginMode == 'boss') {
         var i = e.currentTarget.id
         console.log('this.data==', this.data)
         var list = this.data.goodsCateList
         var that = this
         var param = {
            uid: appData.shopInfo.Biz_ID,
            sql: 'delete from',
            cate_id: list[i].cateId
         }
         app.getJsonData('api/admin/pro_cate_save.php', param, function (res) {
            if (res.code) {
               that.toast(res.message)
            } else {
               list.splice(i, 1)
               that.setData({ goodsCateList: list })
            }
         })
      } else {
         this.toast('需要掌柜账号才能删除')
      }
   },

   toast: function (str) {
      wx.showToast({
         title: str,
         image: '/pages/images/qzg_logo_90x.jpg',
      })
   },

   handleGoodsData: function (goodsData) {
      if (goodsData.code == 0) {
         this.setData({
            goodsCateList: goodsData.data
         })
         appData.goodsCateList = goodsData.data
      }
      console.log(this.data.goodsData)
   },
   /**
  * 生命周期函数--监听页面初次渲染完成
  */
   onReady: function () {
      console.log('生命周期函数--监听页面初次渲染完成')
   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {
      console.log('生命周期函数--监听页面显示')
      this.update()
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {
      console.log('生命周期函数--监听页面隐藏')
   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {
      console.log('生命周期函数--监听页面卸载')
   },
})
